package com.breakfast.fun.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.breakfast.fun.App;
import com.breakfast.fun.R;
import com.breakfast.fun.data.entity.home.ActivityAd;
import com.breakfast.fun.event.UpdateEvent;
import com.breakfast.fun.ui.web.WebActivity;
import com.breakfast.fun.util.DensityUtil;
import com.breakfast.fun.util.ImageUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.ScreenUtil;
import com.breakfast.fun.util.SharedUtil;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeAdActivity extends Activity {

    @Bind(R.id.iv_home_ad)
    ImageView ivHomeAd;
    @Bind(R.id.iv_home_ad_close)
    ImageView ivHomeAdClose;
    @Bind(R.id.rl_home_ad)
    RelativeLayout rlHomeAd;

    @Bind(R.id.iv_ad_guide)
    ImageView ivAdGuide;

    private Tracker mTracker;

    private static int count = 0;  //  点击引导页次数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_ad);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.main_color));
        }
        ButterKnife.bind(this);
        mTracker = App.getApp().getDefaultTracker();
        initView();
    }

    public void initView() {

        getWindow().setLayout(LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT);
        ActivityAd ad = (ActivityAd) getIntent().getSerializableExtra("ad");

        ivHomeAdClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });


        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivHomeAd.getLayoutParams();
        params.width = (int) (ScreenUtil.getScreenWidth(this) * 0.8);
        params.height = (params.width / 13) * 20;
        ivHomeAd.setLayoutParams(params);

        ivAdGuide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count++;
                if (count == 1) {
                    ivAdGuide.setImageResource(R.drawable.bg_yindao2);
                } else if (count == 2) {
                    HomeAdActivity.this.finish();
                    overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
                }
            }
        });

        if (ad != null) {
            new ImageUtil(this).loadUrlImage(ad.getPicture(), ivHomeAd);
        }

        //跳转页面
        ivHomeAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTracker.send(new HitBuilders.EventBuilder()
                        .setCategory("Action")
                        .setAction("Android-点击弹窗")
                        .build());
                if (ad.getTypeLable().equals("ad") && ad.getLinkUrl() != null && !ad.getLinkUrl().equals("")) {
                    String url = ad.getLinkUrl();
                    String title = ad.getTitle();
                    Intent intent = new Intent(HomeAdActivity.this, WebActivity.class);
                    intent.putExtra("url", url);
                    intent.putExtra("title", title);
                    intent.putExtra("des", ad.getInfo());
                    startActivity(intent);
                }
            }
        });

    }

    @Override
    public void finish() {
        Boolean isNeedHelp = SharedUtil.getBoolean(this, "isFirstHelp", true);
        if (isNeedHelp) {
            SharedUtil.putBoolean(this, "isFirstHelp", false);
            ivAdGuide.setVisibility(View.VISIBLE);
            rlHomeAd.setVisibility(View.GONE);
            ivHomeAdClose.setVisibility(View.GONE);
        } else {
            super.finish();
            overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
            EventBus.getDefault().post(new UpdateEvent());
        }
    }
}
